package com.salton123.voice.recorder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.List;

public class UniteUtil {
    public static final String SUFFIX_AMR = ".amr";
    /*
     * 需求:将两个amr格式音频文件合并为1个
     * 注意:amr格式的头文件为6个字节的长度
     * @param partsPaths       各部分路径
     * @param unitedFilePath   合并后路径
     */
    public void uniteAMRFile(String[] partsPaths, String unitedFilePath) {
        try {
            File unitedFile = new File(unitedFilePath);
            FileOutputStream fos = new FileOutputStream(unitedFile);
            RandomAccessFile ra = null;
            for (int i = 0; i < partsPaths.length; i++) {
                ra = new RandomAccessFile(partsPaths[i], "r");
                if (i != 0) {
                    ra.seek(6);
                }
                byte[] buffer = new byte[1024 * 8];
                int len = 0;
                while ((len = ra.read(buffer)) != -1) {
                    fos.write(buffer, 0, len);
                }
            }
            ra.close();
            fos.close();
        } catch (Exception e) {
        }
    }


    public static File getUniteAmrAudioFile(List<String> toUniteFileList, String toSaveDir) {
        if (!toSaveDir.endsWith(File.separator)) {
            toSaveDir = toSaveDir + File.separator;
        }
        String saveFilePath = toSaveDir + System.currentTimeMillis() + SUFFIX_AMR;
        FileOutputStream fileOutputStream;
        try {
            File saveFile = new File(saveFilePath);
            fileOutputStream = new FileOutputStream(saveFilePath);
            // list里面为暂停录音 所产生的 几段录音文件的名字，中间几段文件的减去前面的6个字节头文件
            for (int i = 0; i < toUniteFileList.size(); i++) {
                File file = new File(toUniteFileList.get(i));
                try {
                    FileInputStream fileInputStream = new FileInputStream(file);
                    byte[] myByte = new byte[fileInputStream.available()];
                    // 文件长度
                    int length = myByte.length;
                    // 头文件
                    if (i == 0) {
                        while (fileInputStream.read(myByte) != -1) {
                            fileOutputStream.write(myByte, 0, length);
                        }
                    } else { // 之后的文件，去掉头文件就可以了
                        while (fileInputStream.read(myByte) != -1) {
                            fileOutputStream.write(myByte, 6, length - 6);
                        }
                    }
                    fileOutputStream.flush();
                    fileInputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            // 结束后关闭流
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return saveFile;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
